package com.kele.message.response.read;

import com.kele.message.base.ResponseFrame;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;

/**
 * 读取请求的应答帧，即01,02,03,04功能码的响应帧
 */
public class ReadResponseFrame extends ResponseFrame {
    /**
     * 长度
     */
    private int length;
    /**
     * 内容
     */
    private ByteBuf content;

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public ByteBuf getContent() {
        return content;
    }

    public void setContent(ByteBuf content) {
        this.content = content;
    }

    @Override
    public void decode(ByteBuf in, int addressCode, int functionCode) {
        short length = in.readUnsignedByte();

        this.setFunctionCode(functionCode);
        this.setAddressCode(addressCode);
        this.setLength(length);
        this.setContent(in.readRetainedSlice(length));
        this.setCheckCode(in.readRetainedSlice(2));
    }

    @Override
    public void release() {
        super.release();
        content.release();
    }

    @Override
    public String toString() {
        return "ReadResponseFrame{" +
                "length=" + length +
                ", content=" + ByteBufUtil.hexDump(content) +
                "} " + super.toString();
    }
}
